package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.Product;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.Star;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.StarMapper;
import com.trace.app.service.ProductService;
import com.trace.app.service.StarService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Star)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-22 09:44:22
 */
@Service("starService")
public class StarServiceImpl extends ServiceImpl<StarMapper, Star> implements StarService {

    @Resource
    private ProductService productService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Star> queryWrapper = new LambdaQueryWrapper<>();
        Page<Star> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Star> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer userId, Integer proId) {
        LambdaQueryWrapper<Star> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Star::getUserId, userId)
                .eq(Star::getProId, proId);
        Star one = getOne(queryWrapper);
        if (one != null) {
            return ResponseResult.okResult(one);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.OBJECT_IS_NULL);
        }
    }

    @Override
    public ResponseResult insertOne(Star star) {
        return save(star) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Star star) {
        LambdaUpdateWrapper<Star> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Star::getId, star.getId());
        // TODO
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer userId, Integer proId) {
        LambdaQueryWrapper<Star> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Star::getUserId, userId)
                .eq(Star::getProId, proId);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getStarsByUserId(Integer userId) {
        LambdaQueryWrapper<Star> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Star::getUserId, userId)
                .orderByDesc(Star::getCreateTime);
        List<Star> starList = list(queryWrapper);
        List<Product> collect = starList.stream().map(star -> {
            LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Product::getId, star.getProId());
            return productService.getOne(wrapper);
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }
}

